package com.py.gs.businese.unionbattletest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public abstract class UnionBattleService {
	
	int battleCount;
	
	Random random = new Random();
//	Random random = new Random(156121324);//UnionBattleService1 块
//	Random random = new Random(156121320);//UnionBattleService1 慢
	
	List<UnionBattleInfo> unionBattles = new ArrayList<>();
	
	class UnionBattleInfo {
		long unionId;
		ArrayList<MinisterBattleInfo> ministers = new ArrayList<>();
		int winCount = 0;
		public UnionBattleInfo(long unionId, int ministerNum, int powerBound) {
			this.unionId = unionId;
			for (int i = 0; i < ministerNum; i++) {
				ministers.add(new MinisterBattleInfo(i + 1, powerBound));
			}
		}
		
		@Override
		public String toString() {
			return "unionId:" + unionId + "_winCount:" + winCount;
		}
	}
	class MinisterBattleInfo {
		int ministerId;
		long initMinisterPower;
		long ministerLeftPower;
		int ministerWinCount = 0;
		public MinisterBattleInfo(int ministerId, int powerBound) {
			this.ministerId = ministerId;
			initMinisterPower = 100 + random.nextInt(powerBound);
			ministerLeftPower = initMinisterPower;
		}
		
		public void resetPower() {
			ministerLeftPower = initMinisterPower;
			ministerWinCount = 0;
		}
		
		@Override
		public String toString() {
			return "mId:" + ministerId + "_power:" + initMinisterPower;
		}
	}

	
	/**
	 * 初始化联盟出战信息
	 * @param unionNum 联盟个数
	 * @param ministerNum 联盟出战大臣个数
	 * @param powerBound 军事势力浮动值
	 * 军事势力 = 基数100 + random.nextInt(powerBound)
	 */
	public UnionBattleService(int unionNum, int ministerNum, int powerBound) {
//		System.out.println("初始化" + unionNum + "个战斗联盟");
		for (int i = 0; i < unionNum; i++) {
			unionBattles.add(new UnionBattleInfo(i + 1, ministerNum, powerBound));
		}
//		System.out.println("初始化联盟出战信息...end");
	}
	
	public int battle() {
//		System.out.println("联盟战斗测试");
		int winCount = 0;
		for (int i = 0; i < unionBattles.size(); i++) {
			UnionBattleInfo attack = unionBattles.get(i);
			winCount += battleRandomDefend1(attack, i);
//			winCount += battleRandomDefend2(attack);
		}
		unionBattles.sort((u1, u2)->Integer.compare(u1.winCount, u2.winCount));
		return winCount;
//		System.out.println("联盟战斗胜利次数榜单:" + unionBattles);
//		System.out.println("联盟战斗测试...end");
	}
	private int battleRandomDefend1(UnionBattleInfo attack, int attackIndex) {
		int winCount = 0;
		int[] randomIndexs = randomArray(0, 499, 101);//随机101个防守联盟，多随机一个防止随机到自己
		for (int j = 0; j < 100; j++) {
			int randomIndex = randomIndexs[j];
			if (attackIndex == randomIndex) {//如果是自己，直接取最后一个
				randomIndex = randomIndexs[100];
			}
			UnionBattleInfo defend = unionBattles.get(randomIndex);
			boolean isWin = battle(attack, defend);
			if (isWin) {
				attack.winCount++;
				winCount++;
			} else {
				break;
			}
		}
		return winCount;
	}
	
	public int[] randomArray(int min, int max, int n) {
		int len = max - min + 1;

		if (max < min || n > len) {
			return null;
		}

		// 初始化给定范围的待选数组
		int[] source = new int[len];
		for (int i = min; i < min + len; i++) {
			source[i - min] = i;
		}

		int[] result = new int[n];
		int index = 0;
		for (int i = 0; i < result.length; i++) {
			// 待选数组0到(len-2)随机一个下标
			index = random.nextInt(len--);
			// 将随机到的数放入结果集
			result[i] = source[index];
			// 将待选数组中被随机到的数，用待选数组(len-1)下标对应的数替换
			source[index] = source[len];
		}
		return result;
	}
	
	protected abstract boolean battle(UnionBattleInfo attack, UnionBattleInfo defend);
}
